package ci.web.codec;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpRequest;

/**
 * 域名恶意指向/refer规则不允许
 * @author zhh
 */
public class CiSafeException extends Exception {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public static final String BAD_HOST = "bad_host";
    public static final String BAD_REFERER = "bad_referer";
    
    private HttpRequest request;
    private String type;
    private CiSafeException(String type, String message, HttpRequest request) {
        super(type+':'+message);
        this.request = request;
        this.type = type;
    }
    public String type(){
        return type;
    }
    public HttpRequest request(){
        return request;
    }
    
    /**
     * host不正确的错误
     * @param request
     * @return
     */
    public static CiSafeException byHost(HttpRequest request){
        return new CiSafeException(BAD_HOST, request.headers().getAsString(HttpHeaderNames.HOST), request);
    }
    /**
     * referer不正确的错误
     * @param request
     * @return
     */
    public static CiSafeException byReferer(HttpRequest request){
        return new CiSafeException(BAD_REFERER, request.headers().getAsString(HttpHeaderNames.REFERER), request);
    }
}
